#pragma GCC optimize(2)
#include <iostream>
#include <algorithm>
#include <cstring>
#include <string>
#include <map>
#include <unordered_map>
#include <vector>
#include <set>
#include <queue>

using namespace std;
using LL = long long;
using PII = pair<int, int>;
const int N = 20;


int h, w;
int g[N][N];
unordered_map<int, int> mp;
int cnt[N][N];
int dis[2][2] = {{0, 1}, {1, 0}};
vector<PII> path;

void dfs(int x, int y){
    path.push_back({x, y});
    mp[g[x][y]] = true;
    cnt[x][y] ++;

    // if(x == h && y == w){
    //     for(int i = 0; i < path.size(); i ++){
    //         cout << "x: " << path[i].first << " y: " << path[i].second << "   ";
    //     }
    //     cout << '\n';
    //     cout << cnt[h][w] << '\n';
    // }

    for(int i = 0; i < 2; i ++){
        int nx = x + dis[i][0], ny = y + dis[i][1];
        if(nx < 1 || nx > h || ny < 1 || ny > w) continue;
        if(mp[g[nx][ny]]) continue;

        dfs(nx, ny);
    }    

    mp[g[x][y]] = false;
    path.pop_back();
}

int main(){
#ifdef LOCALFLAG
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);
#endif


    ios::sync_with_stdio(false);
    cin.tie(nullptr);
     
    cin >> h >> w;
    for(int i = 1; i <= h; i ++){
        for(int j = 1; j <= w; j ++){
            cin >> g[i][j];
        }
    }

    dfs(1, 1);
    cout << cnt[h][w] << '\n';


    return 0;
}